dojo.require("dijit.dijit"); // optimize: load dijit layer
dojo.require("dijit._Calendar");
dojo.require("dijit.form.Button");
dojo.require("dijit.form.CheckBox");
dojo.require("dojo.date.locale");
dojo.require("dijit.layout.AccordionContainer");
dojo.require("dijit.layout.AccordionPane");
dojo.require("dijit.ProgressBar");
dojo.require("dijit.Dialog");
dojo.require("dojo.parser"); // scan page for widgets

var days;

dojo.addOnLoad( function()
{
	dijit.byId('calendar1').attr('value', start_date);
	dijit.byId('calendar1').previousYearLabelNode.style.visibility = "hidden";
	dijit.byId('calendar1').nextYearLabelNode.style.visibility = "hidden";
	dojo.connect(dijit.byId('calendar1'), "onClick", dijit.byId('calendar1'), function(){
		if (this.currentYearLabelNode.innerHTML != start_date.getFullYear())
		{
			this.attr('value', start_date);
		}
	});
	dialog_wait.closeButtonNode.style.display = "none";
	
	load_days();
	
	// hide loading gif
	window.setTimeout(function()
	{
		document.getElementById('img').style.display='none';
		document.getElementById('form').style.visibility='';
	}, 100);
});

function sendData()
{
	dialog_wait.show();
	var stamp_s = start_date.getFullYear() + '-'
		+(start_date.getMonth()<9?'0':'')
		+ parseInt(start_date.getMonth()+1) + '-'
		+(start_date.getDate()<10?'0':'') + start_date.getDate();
	
	var stamp_e = end_date.getFullYear() + '-'
		+(end_date.getMonth()<9?'0':'')
		+ parseInt(end_date.getMonth()+1) + '-'
		+(end_date.getDate()<10?'0':'') + end_date.getDate();

	// stamp1,stamp2
	if (dijit.byId('global_length_half').attr('checked'))
	{
		stamp_e = 'half';
	}
	var data = stamp_s+'%2C'+stamp_e;
	dojo.xhrGet({
	    url: "html?action=new&data="+data+"&mdl=event",
	    handleAs: "text",
	    //timeout: 30000, infinity
	    load: function(rdata, ioArgs){
	        alert (rdata); // lesen
	    	dialog_wait.hide();
	        return rdata;
	    },
	    error: function(response, ioArgs){
	    	alert ("Error while saving data. Please try again later. ("+arguments[0]+")");
	    	dialog_wait.hide();
	        return response;
	    }
	});
}

function load_days()
{
	dojo.byId('overlapping').style.display = 'none';
	dojo.byId('legende').style.display = '';
	
	/*
	 * check if end < start
	 */
	end_date = dijit.byId('calendar1').attr('value');
	end_date.setSeconds(0);
	end_date.setHours(0);
	end_date.setMilliseconds(0);

	if (end_date.getTime() < start_date.getTime())
	{
		dijit.byId('calendar1').attr('value', start_date);
		end_date = start_date;
	}

	/*
	 * save the days stamp
	 */
	var iDate = new Date();
	iDate.setTime(start_date.getTime());

	days = 0;
	var shift = 1000*60*60*24 - 60*1000*60; // plus 1 Day - 1 hour / one day is a little bit smaller that 24hours ?!
	while (iDate.getTime() <= (end_date.getTime()+shift))
	{
		var stamp_d = iDate.getFullYear() + '-'
			+(iDate.getMonth()<9?'0':'')
			+ parseInt(iDate.getMonth()+1) + '-'
			+(iDate.getDate()<10?'0':'') + iDate.getDate();

		// check if it's a working day
		weekDay = iDate.getDay();
		if (weekDay == 0)
		{
			weekDay = 7;
		}
		weekDay--;
		weekDay = Math.pow(2, weekDay);
		if ((employee_workingDays & weekDay) == weekDay)
		{
			// check if there is a leave
			var leave = false;
			for (var i=0; i<employee_allHolidays_requested.length; i++)
			{
				if (employee_allHolidays_requested[i] == stamp_d)
				{
					leave = true;
					break;
				}
			}
			for (var i=0; i<employee_allHolidays_accepted.length; i++)
			{
				if (employee_allHolidays_accepted[i] == stamp_d)
				{
					leave = true;
					break;
				}
			}
			if (leave)
			{
				// cannot overlap other holidays
				dojo.byId('overlapping').style.display = '';
				dojo.byId('legende').style.display = 'none';
			} else
			{
				// no leave
				// working day
				
				// is there a public holiday
				var pubhol = false;
				for (var i=0; i<employee_allPublicHolidays.length; i++)
				{
					if (employee_allPublicHolidays[i] == stamp_d)
					{
						pubhol = true;
						break;
					}
				}
				if (!pubhol)
				{
					// ... and no public holiday
					days++;
				}
			}
		}

		iDate.setTime(iDate.getTime()+60*60*24*1000); // next day
		
		// Firefox bugfix - one day is a little bit smaller that 24 hours?!
		var _stamp_d;
		do
		{
			_stamp_d = iDate.getFullYear() + '-'
				+(iDate.getMonth()<9?'0':'')
				+ parseInt(iDate.getMonth()+1) + '-'
				+(iDate.getDate()<10?'0':'') + iDate.getDate();

			if (stamp_d == _stamp_d)
			{
				// theoretically impossible, but FF comes here after a few days
				iDate.setTime(iDate.getTime()+60*60*1000);
			}
		} while (stamp_d == _stamp_d);
	}

	calc_remaining_leave();
}

function calc_remaining_leave()
{
	var n = days;
	if (dijit.byId('global_length_half').attr('checked') && n<=1)
	{
		n = 1/2;
	}
	var remain = employee_remaining_leave - n;
	if (dijit.byId('remainingLeave'))
	{
		if (remain < 0)
		{
			dijit.byId('submit').attr('disabled', true);
			dijit.byId('remainingLeave').update({progress: 0});
			dojo.byId('remainingLeave').style.border = "3px solid red";
		} else if (dojo.byId('overlapping').style.display != 'none')
		{
			// overlapping
			dijit.byId('submit').attr('disabled', true);
		} else
		{
			dojo.byId('remainingLeave').style.border = "";
			dijit.byId('submit').attr('disabled', false);
			dijit.byId('remainingLeave').update({progress: remain});
		}
	}
	
	if (n > 1)
	{
		dijit.byId('global_length_half').attr('checked', false);
	}
	dijit.byId('global_length_half').attr('disabled', (n <= 1 ? false : true) );
}

function get_class_for_date(date)
{
	// check if it's a working day
	var weekDay = date.getDay();
	if (weekDay == 0)
	{
		weekDay = 7;
	}
	weekDay--;
	weekDay = Math.pow(2, weekDay);
	if ((employee_workingDays & weekDay) != weekDay)
	{
		return "noWorkingDay";
	}
	
	// check if there is a leave
	var stamp_d = date.getFullYear() + '-'
		+(date.getMonth()<9?'0':'')
		+ parseInt(date.getMonth()+1) + '-'
		+(date.getDate()<10?'0':'') + date.getDate();

	// check if there is a public holiday
	for (var i=0; i<employee_allPublicHolidays.length; i++)
	{
		if (employee_allPublicHolidays[i] == stamp_d)
		{
			return "publicHoliday";
		}
	}
	
	// make a timespan-highlight
	var s = start_date.getTime();
	var e = dijit.byId('calendar1').attr('value').getTime();
	var n = date.getTime();
	if (
		s <= n
		&& e >= n
	) {
		return "timespanDay";
	}

	// check if there is a accepted leave day
	for (var i=0; i<employee_allHolidays_accepted.length; i++)
	{
		if (employee_allHolidays_accepted[i] == stamp_d)
		{
			return "leaveDay_accepted";
		}
	}
	// check if there is a requested leave day
	for (var i=0; i<employee_allHolidays_requested.length; i++)
	{
		if (employee_allHolidays_requested[i] == stamp_d)
		{
			return "leaveDay_requested";
		}
	}
	
	// check if there is a denied leave day
	// after timespan-highlight
	for (var i=0; i<employee_allHolidays_denied.length; i++)
	{
		if (employee_allHolidays_denied[i] == stamp_d)
		{
			return "leaveDay_denied";
		}
	}
}
